//
//  XTXMeViewController.m
//
//  Created by cd on 2018/7/23.
//  Copyright © 2018年 x. All rights reserved.
//

#import "XTXMeViewController.h"
#import "XTXxingaiziliaoViewController.h"
#import "XTXfankuiViewController.h"
#import "XTXHearViewController.h"
#import <Masonry.h>
#import "XTXMeCell.h"
#import "XTXWodeshipingViewController.h"
#import "XTXAlbumViewController.h"
#import "XTXFJViewController.h"
#import "XTXNetworkingPageViewController.h"
@interface XTXMeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UIImageView *headIconImageView;//头像
@property (nonatomic,strong) UILabel *nichengLabel;//用户名
@property (nonatomic,strong) UILabel *editorLabel;//ID号
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) UILabel *IDLabel;
@property (nonatomic, strong) NSArray *arr;


@end
static NSString * const ID =@"XTXMeViewController";
@implementation XTXMeViewController
- (instancetype)init
{
    self = [super init];
    if (self) {
        UITabBarItem* _aitem = [[UITabBarItem alloc]init];
        _aitem.selectedImage =  [[UIImage imageNamed:@"我的on" ] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        _aitem.image = [[UIImage imageNamed:@"我的off"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        _aitem.title = @"我的";
        [_aitem setTitleTextAttributes:@{NSForegroundColorAttributeName:rgbYanse1(0x82e0ff)} forState:UIControlStateNormal];
        [_aitem setTitleTextAttributes:@{NSForegroundColorAttributeName:rgbYanse1(0x5974ff)} forState:UIControlStateSelected];
        self.tabBarItem = _aitem;
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad { 
    [super viewDidLoad];
    self.view.backgroundColor = rgbYanse1(0xf0f0f0);
    [self jiemianchushihua];
}
-(void)jiemianchushihua{
    self.arr=@[
        @[@{
              @"image":@"我的听见",
              @"title":@"我的音遇"
        }
        ],@[
            @{
                @"image":@"相册",
                @"title":@"相册"
            },@{
                @"image":@"视频",
                @"title":@"视频"
            }
        ],@[
            @{
                @"image":@"协议",
                @"title":@"用户协议"
            },@{
                @"image":@"问题反馈",
                @"title":@"问题反馈"
            },@{
                @"image":@"退出登录",
                @"title":@"退出登录"
            }
        ]
    ];
    
    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.top.offset(0);
    }];
    tableView.bounces=NO;
    tableView.rowHeight=60;
    tableView.dataSource=self;
    tableView.delegate=self;
    tableView.showsVerticalScrollIndicator=NO;
    tableView.showsHorizontalScrollIndicator=NO;
    _tableView=tableView;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor=UIColor.clearColor;
    UIView *headView=[[UIView alloc]init];
    CGFloat height=220 * pingmuKuan / 375;
    headView.height=height;
    _tableView.tableHeaderView=headView;
    //背景
    UIImageView *bgImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"我的页面-底图"]];
    bgImageView.contentMode=UIViewContentModeScaleAspectFill;
    bgImageView.layer.masksToBounds=YES;
    [headView addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.bottom.offset(0);
    }];
    //头像背景
    UIImageView *headBgImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"我的-头像地图"]];
    [headView addSubview:headBgImageView];
    headBgImageView.contentMode=UIViewContentModeScaleAspectFill;
    [headBgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(125);
        make.height.offset(125);
        make.centerX.equalTo(bgImageView.mas_centerX);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(headView.mas_safeAreaLayoutGuideTop).offset(5);
        } else {
            make.top.equalTo(headView.mas_top).offset(5);
        }
    }];
    //头像
    UIImageView *headIconImageView=[[UIImageView alloc]init];
    [headView addSubview:headIconImageView];
    [headIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(110);
        make.height.offset(110);
        make.centerX.equalTo(headBgImageView.mas_centerX);
        make.centerY.equalTo(headBgImageView.mas_centerY);
    }];
    headIconImageView.contentMode=UIViewContentModeScaleAspectFill;
    headIconImageView.layer.cornerRadius=55;
    headIconImageView.layer.masksToBounds=YES;
    _headIconImageView=headIconImageView;
    //昵称
    UILabel *nichengLabel=[[UILabel alloc]init];
    nichengLabel.text=@"123";
    [nichengLabel setFont:[UIFont fontWithName:@"PingFangSC-Semibold" size:17]];
    [nichengLabel setTextColor:UIColor.whiteColor];
    [headView addSubview:nichengLabel];
    [nichengLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headBgImageView.mas_bottom).offset(15);
        make.centerX.equalTo(headView.mas_centerX);
    }];
    _nichengLabel=nichengLabel;
    //id
    UILabel *IDLabel=[[UILabel alloc]init];
    IDLabel.text=@"123";
    [IDLabel setFont:[UIFont fontWithName:@"PingFangSC-Regular" size:13]];
    [IDLabel setTextColor:UIColor.whiteColor];
    [headView addSubview:IDLabel];
    [IDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nichengLabel.mas_bottom).offset(10);
        make.centerX.equalTo(headView.mas_centerX);
    }];
    _IDLabel=IDLabel;
    //编辑资料按钮
    UIButton *editBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [headView addSubview:editBtn];
    [editBtn setImage:[UIImage imageNamed:@"编辑资料"] forState:UIControlStateNormal];
    [editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(headView.mas_safeAreaLayoutGuideTop).offset(5);
        } else {
            make.top.equalTo(headView.mas_top).offset(5);
        }
        make.right.offset(-15);
    }];
    [editBtn addTarget:self action:@selector(editBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass(XTXMeCell.class) bundle:nil] forCellReuseIdentifier:ID];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    /// 数据初始化
    [self shujuchushuhua];
    
}
/// 数据初始化
-(void)shujuchushuhua{
    [_headIconImageView sd_setImageWithURL:[NSURL URLWithString:XTXGongTool.share.useMode.headimage]];
    _nichengLabel.text=XTXGongTool.share.useMode.nickname;
    _IDLabel.text=[@"ID:" stringByAppendingString:XTXGongTool.share.useMode.idnum];
}

//编辑资料
-(void)editBtnClick{
    XTXxingaiziliaoViewController *Blacklistinformation = [[XTXxingaiziliaoViewController alloc]init];
    [self.navigationController pushViewController:Blacklistinformation animated:YES];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.arr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *arr=self.arr[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XTXMeCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    NSArray *arr=self.arr[indexPath.section];
    NSDictionary *dic=arr[indexPath.row];
    cell.imageStr=dic[@"image"];
    cell.titleStr=dic[@"title"];
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {//我的听见
        [self jumpToWodetingjianjiemian];
    }else if(indexPath.section==1){
        if (indexPath.row==0) {//相册
            [self tiaozhuandaoxiangce];
        }else{//视频
            [self tiaozhuandaowodeshiping];
        }
    }else{
        if (indexPath.row==0) {//用户协议
            [self tiaozhuandaoxieyi];
        }else if (indexPath.row==1){//问题反馈
            [self tiaozhuandaofankui];
        }else{//退出登录
            [self tuichudenglu];
        }
    }
}
///我的听见
-(void)jumpToWodetingjianjiemian{
    XTXHearViewController *newHomeViewContr = [[XTXHearViewController alloc] init];
    newHomeViewContr.typeInt = 3;
    [self.navigationController pushViewController:newHomeViewContr animated:YES];
}
///退出登录
-(void)tuichudenglu{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定退出登录吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *cancelAxtion = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSNotification * notice = [NSNotification notificationWithName:@"stopMusic" object:nil userInfo:nil];
        [[NSNotificationCenter defaultCenter]postNotification:notice];
        NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults];
        [defatluts setObject:@"0" forKey:@"loginStatus"];
        [defatluts synchronize];
        [XTXGongView denglujiemiandezhanshiEventClick];
    }];
    [alertController addAction:cancelAxtion];
    [alertController addAction:sureAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
///反馈
-(void)tiaozhuandaofankui{
    XTXfankuiViewController * FeedbackViewinformation = [[XTXfankuiViewController alloc]init];
    [self.navigationController pushViewController:FeedbackViewinformation animated:YES];
}
///用户协议
-(void)tiaozhuandaoxieyi{
    XTXNetworkingPageViewController *web=[[XTXNetworkingPageViewController alloc]init];
    web.dizhiStr=xieyiUrl;
    [self.navigationController pushViewController:web animated:YES];
}

/// 相册
-(void)tiaozhuandaoxiangce{
    XTXAlbumViewController *myPhotViewC = [[XTXAlbumViewController alloc] init];
    myPhotViewC.selectedNum=meControllerSelected;
    [self.navigationController pushViewController:myPhotViewC animated:YES];
}

/// 视频
-(void)tiaozhuandaowodeshiping{
    XTXWodeshipingViewController *videoVc = [[XTXWodeshipingViewController alloc] init];
    [self.navigationController pushViewController:videoVc animated:YES];
}

-(void)chongxinqingqiuyonghuxinxi{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    [XTXNetworking networkingGetUseInfowangluoqingqiu:dic completionHandler:^(NSDictionary * _Nonnull dict) {
        XTXGongTool.share.useMode=[XTXUserModel mj_objectWithKeyValues:dict[@"info"]];
    }];
}
@end
